#include <STC15F2K60S2.H>
#include "Tim.h"
#include "Seg.h"
#include "Key.h"
#include "Ds1302.h"
#include "stdio.h"
#include "Onewire.h"
#include "iic.h"
#include "Led.h"
unsigned long ulms,ucStart;
unsigned char pucSeg_Buf[10],pucSeg_Code[8],ucSeg_Pos;
unsigned int ucSeg_Dly;
unsigned char ucKey_Dly,ucKey_Old;
unsigned char ucState,ucState_Flag;
unsigned char pucRtc[3]={16,59,50};
unsigned char ucLeds=4,ucTimes=17;
unsigned int ucTemp=25;
unsigned int uiTemp;
unsigned char ucpcf_U,ucpcf_Flag,ucpcf_Old;
unsigned char ucLed;
void Key_Proc(void) ;
void Seg_Proc(void);
void Set_Proc(unsigned char ucKey_Down);
void Led_Proc(void);
//注意灯设置时候的转换状态，要不要先把他清灭在设置（一般用于两个灯以上）
//一些变量会自己变成0，有这个问题要注意好
void main(){
	Cls_Peripheral() ;
	Timer1Init();
	Set_RTC(pucRtc);
	while(1){
		Key_Proc() ;
		Seg_Proc();
		Led_Proc();
}
}
void Time_1(void) interrupt 3 //定时器1中断函数
{ 
	 ulms++; 
	 Led_Disp(ucLed) ;
	 if(++ucKey_Dly==10) ucKey_Dly=0;
	if(++ucSeg_Dly==500) ucSeg_Dly=0;
	 Seg_Disp(pucSeg_Code, ucSeg_Pos);
	if(++ucSeg_Pos==8) ucSeg_Pos=0;
}
void Key_Proc(void) 
{ 
	 unsigned char ucKey_Val, ucKey_Down, ucKey_Up; 
	 if(ucKey_Dly) return; 
		ucKey_Dly = 1; 
	 ucKey_Val = Key_Read(); 
	 ucKey_Down = ucKey_Val & (ucKey_Old ^ ucKey_Val); 
	 ucKey_Up = ~ucKey_Val & (ucKey_Old ^ ucKey_Val); 
	 ucKey_Old = ucKey_Val; 
	 switch(ucKey_Down){
		 case 4:
			 ucState_Flag^=1;
			ucState=0;
			 break;
		 case 5:
			 if(++ucState==3) {
				 ucState=0;
			 }
			 break;
		 case 8:
			 Set_Proc(ucKey_Down);
			 break;
		  case 9:
			 Set_Proc(ucKey_Down);
			 break;
	 }
}
void Seg_Proc(void){
	if(ucSeg_Dly) return;
	ucSeg_Dly=1;
	ucpcf_U=PCF8591_Adc();
	if(ucpcf_U==0){
		ucpcf_Flag=1;
	}
	else{
		ucpcf_Flag=0;
	}
	if(ucpcf_Flag!=ucpcf_Old){
		ucpcf_Old=ucpcf_Flag;
		ucStart=ulms;
	}
	uiTemp=rd_temperature();
	Read_RTC(pucRtc) ;
	if(!ucState_Flag){
		switch(ucState){
			case 0:
				sprintf(pucSeg_Buf,"%02d-%02d-%02d",(unsigned int)pucRtc[0],(unsigned int)pucRtc[1],(unsigned int)pucRtc[2]);
				break;
			case 1:
				sprintf(pucSeg_Buf,"C    %3.2f",uiTemp/16.0);
				break;
			case 2:
				sprintf(pucSeg_Buf,"E %3.2f  %1d",ucpcf_U/51.0,(unsigned int)ucpcf_Flag);
				break;
		}
	}
	else{
		switch(ucState){
			case 0:
				sprintf(pucSeg_Buf,"P1    %02d",(unsigned int)ucTimes);
				break;
			case 1:
				sprintf(pucSeg_Buf,"P2    %02d",ucTemp);
				break;
			case 2:
				sprintf(pucSeg_Buf,"P3     %d", (unsigned int)ucLeds);
				break;
		}
	}
	Seg_Tran(pucSeg_Buf, pucSeg_Code);
}
void Set_Proc(unsigned char ucKey_Down){
	if(ucState_Flag){
		switch(ucState){
			case 0:
				if(ucKey_Down==8){
					if(ucTimes==0){
						ucTimes=24;
					}
					ucTimes--;
				}
				else if(ucKey_Down==9){
					if(++ucTimes==24){
						ucTimes=0;
					}
				}
				break;
				case 1:
				if(ucKey_Down==8){
					if(ucTemp==0){
						ucTemp=100;
					}
					ucTemp--;
				}
				else if(ucKey_Down==9){
					if(++ucTemp==100){
						ucTemp=0;
					}
				}
				break;
				case 2:
				if(ucKey_Down==8){
					if(--ucLeds==3){
						ucLeds=8;
					}
				}
				else if(ucKey_Down==9){
					if(++ucLeds==9){
						ucLeds=4;
					}
				}
				break;
		}
	}
}
void Led_Proc(void){
	if(!ucState_Flag){
	if(pucRtc[0]>7&&pucRtc[0]<ucTimes){
		ucLed&=~1;
	}
	else{
		ucLed|=1;
	}
	if((uiTemp/16)<ucTemp){
		ucLed|=2;
	}
	else{
		ucLed&=~2;
	}
	if(ucpcf_Flag){
		ucLed&=0x07;
		ucLed|=(0x01<<ucLeds-1);
		}
	else{
		ucLed&=0x07;
			}	
	}
	if(ulms-ucStart>=3000){
		if(ucpcf_Flag){
			ucLed|=4;
		}
		else{
			ucLed&=~4;
		}
	}
}